package neu.competition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import neu.competition.dto.CompetitionsDTO;
import neu.competition.entity.Competitions;
import neu.competition.mapper.CompetitionsMapper;
import neu.competition.service.CompetitionsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompetitionsServiceIml implements CompetitionsService {

    @Autowired
    private CompetitionsMapper competitionsMapper;

    /**
     * 创建赛事
     *
     * @param competitionsDTO
     */
    public void insert(CompetitionsDTO competitionsDTO, String id) {
        Competitions competitions = new Competitions();
        BeanUtils.copyProperties(competitionsDTO, competitions);
        competitions.setManagerid(id);
        competitions.setLastest(0);
        competitionsMapper.insert(competitions);
    }

    /*
     查询某一个管理人员负责的赛事
     */
    @Override
    public List<CompetitionsDTO> getCompetitionSDotListByUserId(String id) {
        QueryWrapper<Competitions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("managerId", id);
        List<Competitions> competitionsList = competitionsMapper.selectList(queryWrapper);
        List<CompetitionsDTO> competitionsDTOS = CompetitionListConversionCompetitionsDTOS(competitionsList);
        return competitionsDTOS;
    }


    public List<CompetitionsDTO> CompetitionListConversionCompetitionsDTOS(List<Competitions> competitionsList) {
//        List<CompetitionsDTO> competitionsDTOS=new ArrayList<>();
//        for (Competitions competitions:competitionsList){
//            CompetitionsDTO dto=new CompetitionsDTO();
//            dto.setComid(competitions.getComid());
//            dto.setComname(competitions.getComname());
//            dto.setComlogo(competitions.getComlogo());
//            dto.setCompurpose(competitions.getCompurpose());
//            dto.setComother(competitions.getComother());
//            dto.setLastest(competitions.getLastest());
//            dto.setSponsor(dto.getSponsor());
//            dto.setComsysld(competitions.getComsysld());
//            dto.setComprofile(competitions.getComprofile());
//            competitionsDTOS.add(dto);
//        }
//        return competitionsDTOS;
        return competitionsList.stream().map(competitions -> {
            CompetitionsDTO dto = new CompetitionsDTO();
            BeanUtils.copyProperties(competitions, dto);
            return dto;
        }).collect(Collectors.toList());
    }


    /*
    查询所有赛事
    */
    public List<CompetitionsDTO> getAllCompetitionsDTOList() {
        QueryWrapper<Competitions> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("lastest");

        List<Competitions> competitionsList = competitionsMapper.selectList(queryWrapper);
        List<CompetitionsDTO> competitionsDTOS = CompetitionListConversionCompetitionsDTOS(competitionsList);
        return competitionsDTOS;
    }


    /*
    获取赛事详情
     */
    @Override
    public CompetitionsDTO getCompetitionDTOSById(Integer id) {
        CompetitionsDTO competitionsDTO = new CompetitionsDTO();
        BeanUtils.copyProperties(competitionsMapper.selectById(id), competitionsDTO);
        return competitionsDTO;
    }

    /**
     * 赛事重复逻辑
     *
     * @param competitionsDTO
     * @return
     */
    @Override
    public boolean isDuplicate(CompetitionsDTO competitionsDTO) {
        QueryWrapper<Competitions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comname", competitionsDTO.getComname());
        Competitions existingCompetition = competitionsMapper.selectOne(queryWrapper);
        return existingCompetition != null;
    }

    @Override
    public List<CompetitionsDTO> getCompetitionSDotList(String name) {
        QueryWrapper<Competitions> queryWrapper = new QueryWrapper();
        queryWrapper.like("comName", name);
        List<Competitions> competitionsList = competitionsMapper.selectList(queryWrapper);

        return CompetitionListConversionCompetitionsDTOS(competitionsList);
    }

    @Override
    public int UpdateCompetitionsLastest(int comId, int matchId) {
        LambdaUpdateWrapper<Competitions> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Competitions::getComid, comId) // 设置条件，例如 id = 1
                .set(Competitions::getLastest, matchId);
        int update = competitionsMapper.update(null, updateWrapper);
        return update;
    }

    @Override
    public List<CompetitionsDTO> getAllCompetitionsDTOListThree() {
        QueryWrapper<Competitions> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("lastest");
        queryWrapper.last("LIMIT 3");
        List<Competitions> competitionsList = competitionsMapper.selectList(queryWrapper);
        List<CompetitionsDTO> competitionsDTOS = CompetitionListConversionCompetitionsDTOS(competitionsList);
        return competitionsDTOS;
    }
}

